﻿using System;
using UnityEditor;
using UnityEngine;

public class MainRoleShaderEditor : ShaderGUI
{

    public enum CullMode
    {
        CULL_NONE = 0,
        CULL_FRONT = 1,
        CULL_BACK = 2,
    }

    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        Shader.SetGlobalFloat(ShaderConst.SID_RoleDistance, 0);

        Material targetMat = materialEditor.target as Material;

        base.OnGUI(materialEditor, properties);
        bool USE_ALPHA_FADE = Array.IndexOf<string>(targetMat.shaderKeywords, "USE_ALPHA_FADE") != -1;
        EditorGUI.BeginChangeCheck();

        var alpha = 1f;
        //var mainRoleAlpha = Shader.GetGlobalFloat(ShaderConst.SID_MainRoleAlpha);
        targetMat.EnableKeyword("USE_TEXTURE");
        for (int i = 0; i < properties.Length; i++)
        {
            var prop = properties[i];
            if (prop.name == "_MainTex")
            {
                bool USE_TEXTURE = prop.textureValue != null;
                if (USE_TEXTURE)
                    targetMat.EnableKeyword("USE_TEXTURE");
                else
                    targetMat.DisableKeyword("USE_TEXTURE");
            }
            else if (prop.name == "_EmissionTex")
            {
                bool USER_EMISSION_TEXTURE = prop.textureValue != null;
                if (USER_EMISSION_TEXTURE)
                    targetMat.EnableKeyword("USER_EMISSION_TEXTURE");
                else
                    targetMat.DisableKeyword("USER_EMISSION_TEXTURE");
            }
            else if (prop.name == "_BumpMap")
            {
                var USE_BUMP_TEXTURE = prop.textureValue != null;
                if (USE_BUMP_TEXTURE)
                    targetMat.EnableKeyword("USE_BUMP_TEXTURE");
                else
                    targetMat.DisableKeyword("USE_BUMP_TEXTURE");
            }
            else if (prop.name == "_Alpha")
            {
                EditorGUILayout.BeginHorizontal();
                USE_ALPHA_FADE = EditorGUILayout.ToggleLeft("透明度", USE_ALPHA_FADE, GUILayout.Width(80));
                if (USE_ALPHA_FADE)
                {
                    alpha = EditorGUILayout.Slider(prop.floatValue, 0f, 1f);
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        //mainRoleAlpha = EditorGUILayout.Slider("主角透明度", mainRoleAlpha, 0f, 1f);

        //cullMode
        var cullMode = FindProperty("_Cull", properties);
        GUILayout.BeginHorizontal();
        GUILayout.Label(new GUIContent("Cull", "CullMode"), GUILayout.Width(120));
        var cull = (CullMode)cullMode.floatValue;
        cull = (CullMode)EditorGUILayout.Popup((int)cull, Enum.GetNames(typeof(CullMode)));
        GUILayout.EndHorizontal();
        bool ALWAYS_FACE_CAMERA = Array.IndexOf<string>(targetMat.shaderKeywords, "ALWAYS_FACE_CAMERA") != -1;
        ALWAYS_FACE_CAMERA = EditorGUILayout.Toggle("法线始终朝向镜头", ALWAYS_FACE_CAMERA);


        if (EditorGUI.EndChangeCheck())
        {
            if (USE_ALPHA_FADE)
                targetMat.EnableKeyword("USE_ALPHA_FADE");
            else
                targetMat.DisableKeyword("USE_ALPHA_FADE");
            if (ALWAYS_FACE_CAMERA)
                targetMat.EnableKeyword("ALWAYS_FACE_CAMERA");
            else
                targetMat.DisableKeyword("ALWAYS_FACE_CAMERA");

            //cullMode
            cullMode.floatValue = (float)cull;
            targetMat.SetInt("_Cull", (int)cull);

            if (USE_ALPHA_FADE)
                targetMat.SetFloat(ShaderConst.SID_Alpha, alpha);

            //Shader.SetGlobalFloat(ShaderConst.SID_MainRoleAlpha, mainRoleAlpha);
        }

    }

}